package com.pubinfo.passbook.common.service.contract.impl;

import cn.hutool.core.date.DateUtil;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.pubinfo.passbook.common.entity.TContractBase;
import com.pubinfo.passbook.common.entity.TContractInvoiceApply;
import com.pubinfo.passbook.common.enums.ContractInvoiceStatusEnum;
import com.pubinfo.passbook.common.enums.ContractStatusEnum;
import com.pubinfo.passbook.common.exception.ServiceException;
import com.pubinfo.passbook.common.mapper.TContractInvoiceApplyMapper;
import com.pubinfo.passbook.common.model.dto.contract.ApplyInvoiceParam;
import com.pubinfo.passbook.common.model.dto.contract.ContractIdParam;
import com.pubinfo.passbook.common.model.dto.contract.FeeListParam;
import com.pubinfo.passbook.common.model.eop.ApplyParam;
import com.pubinfo.passbook.common.model.http.eip.UserInfo;
import com.pubinfo.passbook.common.model.vo.base.BusinessErrorCode;
import com.pubinfo.passbook.common.model.vo.contract.ContractRelateDocVo;
import com.pubinfo.passbook.common.model.vo.contract.FapiaoDetailVo;
import com.pubinfo.passbook.common.model.vo.contract.FeeCollectionListVo;
import com.pubinfo.passbook.common.service.contract.TContractBaseService;
import com.pubinfo.passbook.common.service.contract.TContractInvoiceApplyService;
import com.pubinfo.passbook.common.service.dubbo.DubboService;
import com.pubinfo.passbook.common.utils.BigDecimalUtils;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.Date;
import java.util.List;

/**
 * @author admin
 * @description 针对表【t_contract_invoice_apply(发票申请单)】的数据库操作Service实现
 * @createDate 2021-12-06 14:45:14
 */
@Service
public class TContractInvoiceApplyServiceImpl extends ServiceImpl<TContractInvoiceApplyMapper, TContractInvoiceApply> implements TContractInvoiceApplyService {
  
  @Resource
    private DubboService dubboService;

    @Resource
    private TContractBaseService contractBaseService;

    @Override
    public List<TContractInvoiceApply> getContractApplies(Integer contractId) {
        QueryWrapper<TContractInvoiceApply> qw = new QueryWrapper<>();
        qw.lambda().eq(TContractInvoiceApply::getContractId, contractId);
        return this.baseMapper.selectList(qw);
    }

    //可开票合同查询
    @Override
    public IPage<FeeCollectionListVo> selectUseableList(FeeListParam feeListParam) {
        IPage<FeeCollectionListVo> iPage = feeListParam.getPage();
        return this.baseMapper.selectUseableList(iPage, feeListParam);
    }

    @Override
    public List<FapiaoDetailVo> invoiceDetail(ContractIdParam param) {
        return this.baseMapper.invoiceDetail(param.getContractId());
    }

    @Override
    public Boolean orderApply(UserInfo user, ApplyInvoiceParam param) {
  
      TContractBase contract = contractBaseService.getContractById(param.getContractId());
      //未认领不可开票
      if (ContractStatusEnum.NO_CLAIM == ContractStatusEnum.getEnum(contract.getStatus()))
        throw new ServiceException(BusinessErrorCode.INVOICE_CANT_APPLY);
  
      //可开票金额判断
      boolean modifyInvoice = contractBaseService.modifyInvoiceAmount(param.getContractId(), param.getInvoiceAmount());
      if (!modifyInvoice)
        throw new ServiceException(BusinessErrorCode.INVOICE_AMOUNT_NOT_ENOUGH);
  
      //   String applyId = String.valueOf(SnowflakesTools.getNextId());
      String applyId = "bill" + DateUtil.format(new Date(), "yyMMddHHmmssSSS");
      ApplyParam applyParam = new ApplyParam();
      applyParam.setContractId(param.getContractId());//合同ID
      applyParam.setInvoiceApplyId(applyId);//申请单ID
      applyParam.setAmount(param.getInvoiceAmount());//开票金额
      applyParam.setCheckPre(true);//是否校验
  
      //预检
  
      //预检
      String applyResult = dubboService.orderApply(applyParam);
      if (!"SUCCESS".equals(applyResult))
        throw new ServiceException(BusinessErrorCode.pubError(applyResult));
      TContractInvoiceApply apply = new TContractInvoiceApply();
      apply.setInvoiceApplyId(applyId);//发票申请流水号
      apply.setContractId(contract.getContractId());//合同流水号
      apply.setPayerName(param.getPartbName());//对方单位
      apply.setTin(param.getSocialCode());//纳税人识别号
      apply.setAddressTel(param.getConsAddr());//地址 电话
      apply.setBank(contract.getBankName());//开户行
      apply.setAccount(contract.getBankAccounts());//账号
      apply.setInvoiceAmount(param.getInvoiceAmount());//开票金额
      apply.setTaxRate(param.getInvoiceRate());//税率
      apply.setPerformanceId(param.getPerformanceId());
      apply.setTaxAmount(BigDecimalUtils.multiply(param.getInvoiceAmount(), param.getInvoiceRate()));//税金
      apply.setAmount(param.getInvoiceAmount().subtract(apply.getTaxAmount()));//不含税金额
      apply.setStatus(ContractInvoiceStatusEnum.APPLY.getStatus());//状态
      apply.setCreateTime(new Date());//创建时间
      apply.setCreateUser(user == null ? "" : user.getUsername());//创建人
      //this.baseMapper.insert(apply);
      save(apply);
      //正式单
      applyParam.setCheckPre(false);//是否校验
      applyResult = dubboService.orderApply(applyParam);
      if (!"SUCCESS".equals(applyResult)) {
        apply.setStatus(ContractInvoiceStatusEnum.ERROR.getStatus());//状态
    
        updateById(apply);
        throw new ServiceException(BusinessErrorCode.pubError(applyResult));
    
      }
      return true;
  
    }

    /**
     * 获取合同相关单据
     */
    @Override
    public List<ContractRelateDocVo> getContractRelateDoc(Integer contractId) {
        return this.baseMapper.getContractRelateDoc(contractId);
    }
}




